package com.admin.education.controllers.api.v1;

import com.admin.education.common.response.IResponseMessage;
import com.admin.education.common.response.ResponseMessageWithData;
import com.admin.education.common.response.Result;
import com.admin.education.common.exceptions.BadRequestException;
import com.admin.education.common.jwt.JwtAuthorize;
import com.admin.education.common.jwt.JwtUtils;
import com.admin.education.dataTransferObject.PageRequest;
import com.admin.education.dataTransferObject.PageResponse;
import com.admin.education.dataTransferObject.ServicesExcuteResult;
import com.admin.education.dataTransferObject.userAnswerSheet.UserAnswerSheetTransfer;
import com.admin.education.dataTransferObject.userInformation.UserInformationTransfer;
import com.admin.education.services.Interfaces.IUserService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.concurrent.Callable;

@RestController
@RequestMapping("/v1/userAnswerSheets")
@JwtAuthorize
public class UserAnswerSheetController {
    @Autowired
    private IUserService userService;
    @GetMapping("")
    public Callable<IResponseMessage> queryUserAnswerSheets(@Valid PageRequest pageRequest, HttpServletRequest request)
    {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;
                String auth =JwtUtils.getClaims(request,"auth").toString();

                if(null==auth)
                    throw new BadRequestException("请先登录");

                UserInformationTransfer userInformationTransfer = JSONObject.parseObject(auth,UserInformationTransfer.class);


                ServicesExcuteResult<PageResponse<UserAnswerSheetTransfer>> result = userService.queryUserAnswerSheetByUserId(userInformationTransfer.getId(),pageRequest);
                if(result.isSuccess()){
                    responseMessage = new ResponseMessageWithData<PageResponse<UserAnswerSheetTransfer>>(Result.Success,result.getData());
                }
                else{
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
}
